import httplib, urllib, base64, json
import sys

###############################################
#### Update or verify the following values. ###
###############################################

# Replace the subscription_key string value with your valid subscription key.
subscription_key = 'microsoft azure subscription key'

# Replace or verify the region.
#
# You must use the same region in your REST API call as you used to obtain your subscription keys.
# For example, if you obtained your subscription keys from the westus region, replace 
# "westcentralus" in the URI below with "westus".
#
# NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using
# a free trial subscription key, you should not need to change this region.
uri_base = 'eastus2.api.cognitive.microsoft.com'

# Request headers.
headers = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': subscription_key,
        }

# Request parameters.
params = urllib.urlencode({
        'returnFaceId': 'false',
        'returnFaceLandmarks': 'false',
        'returnFaceAttributes': 'age,gender',
        })

def getUser(path):
    ##screenName = sys.argv[1]
    screenName = path
    body = "{'url':'https://twitter.com/%s/profile_image?size=original'}" % screenName
    ##print body
    # The URL of a JPEG image to analyze.
    ##body = "{'url':'https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg'}"
    result =[]
    try:
        # Execute the REST API call and get the response.
        conn = httplib.HTTPSConnection(uri_base)
        conn.request("POST", "/face/v1.0/detect?%s" % params, body, headers)
        response = conn.getresponse()
        data = response.read()
        # 'data' contains the JSON data. The following formats the JSON data for display.
        parsed = json.loads(data)
        for ele in parsed:
            result.append(ele['faceAttributes'])
        
        #print result
        #return result
        ##result = parsed[0]['faceAttributes']
        #print result["age"]
        #print result["gender"]
        #print parsed
        conn.close()
        return result
    except Exception as e:
        #print 'something wrong'
        #print("[Errno {0}] {1}".format(e.errno, e.strerror))
        return result

